package cc.fine.svc.main.listener;

import cc.fine.svc.common.exception.FineException;
import cc.fine.svc.common.utils.R;
import cc.fine.svc.config.mq.RabbitmqSyncConfig;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.TagWeight;
import cc.fine.svc.main.bean.po.*;
import cc.fine.svc.main.bean.vo.CommonPublishVo;
import cc.fine.svc.main.service.RelUserVideoHistoryService;
import cc.fine.svc.main.service.WeightService;
import cc.fine.svc.main.service.rel.RelUserFanService;
import cc.fine.svc.main.service.rel.RelUserVideoCollectService;
import cc.fine.svc.main.service.rel.RelUserVideoFavorService;
import cc.fine.svc.main.service.rel.RelUserVideoShareService;
import cc.fine.svc.main.service.tbl.TblCommonService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.qiniu.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/10/28
 * <p>
 * 监听处理异步任务
 */
@Component
@RabbitListener(queues = RabbitmqSyncConfig.QUEUE)
@Slf4j
public class MqListener {

    @Autowired
    private RelUserVideoFavorService favorService;

    @Autowired
    private TblCommonService commonService;

    @Autowired
    private RelUserVideoShareService shareService;

    @Autowired
    private RelUserVideoHistoryService historyService;

    @Autowired
    private RelUserFanService fanService;

    @Autowired
    private WeightService weightService;

    @Autowired
    private RelUserVideoCollectService videoCollectService;


    @RabbitHandler
    public void receive(String json) {
        try {
            run(json);
        } catch (Exception e) {
            log.error("MqListener 监听器出现异常");
            e.printStackTrace();
        }
    }

    private void run(String json) {
        Gson gson = new Gson();
        FineMessage message = gson.fromJson(json, FineMessage.class);

        String type = message.getType();
        if (type == null) {
            throw new FineException(R.ERROR, "MqListener => FineMessage 中 type 参数为空");
        }


        switch (type) {

            case FineMessage.FAVOR:
                log.info("MqListener => 给视频点赞：{}", message);
                favor(message);
                break;

            case FineMessage.DIS_FAVOR:
                log.info("MqListener => 取消视频的点赞: {}", message);
                disFavor(message);
                break;

            case FineMessage.PUBLISH_COMMON:
                log.info("MqListener => 发表评论：{}", message);
                publishCommon(message);
                break;

            case FineMessage.VIDEO_SHARE:
                log.info("MqListener => 分享视频：{}", message);
                share(message);
                break;

            case FineMessage.VIDEO_VIEW_HISTORY:
                log.info("MqListener => 观看了一个视频：{}", message);
                view(message);
                break;

            case FineMessage.USER_FANS_OP:
                log.info("MqListener => 关注用户: {}", message);
                follow(message);
                break;

            case FineMessage.Dis_User_Fans_Op:
                log.info("MqListener => 取消关注用户: {}", message);
                disFollow(message);
                break;

            case FineMessage.Weight:
                log.info("MqListener => 处理用户权重: {}", message);
                weight(message);
                break;

            case FineMessage.Collect_Add:
                log.info("MqListener => 收藏视频: {}", message);
                videoCollect(message);
                break;

            case FineMessage.Dis_Collect_Add:
                log.info("MqListener => 取消收藏视频: {}", message);
                videoDisCollect(message);
                break;



            default:
                throw new FineException(R.ERROR, "mq接收器找不到type: " + type);

        }
    }

    private void videoDisCollect(FineMessage message) {
        String collectId = message.getCollectId();
        String videoId = message.getVideoId();
        videoCollectService.disCollect(collectId, videoId);

    }

    private void disFollow(FineMessage message) {
        RelUserFan fan = message.getUserFan();
        fanService.disFollow(fan);

    }

    private void videoCollect(FineMessage message) {
        String collectId = message.getCollectId();
        String videoId = message.getVideoId();

        videoCollectService.collect(collectId, videoId);

    }

    private void weight(FineMessage message) {
        TagWeight weight = message.getWeight();
        weightService.save(weight);
    }

    private void follow(FineMessage message) {
        RelUserFan fan = message.getUserFan();
        fanService.follow(fan);
    }

    private void view(FineMessage message) {
        RelUserVideoHistory history = message.getHistory();
        historyService.view(history);
    }

    /**
     * 对应——给视频点赞
     *
     */
    private void favor(FineMessage message) {
        RelUserVideoFavor favor = message.getFavor();
        favorService.favor(favor);
    }

    /**
     * 对应——发表评论
     */
    private void publishCommon(FineMessage message) {
        CommonPublishVo commonPublish = message.getCommonPublish();
        commonService.publish(commonPublish);
    }

    /**
     * 取消视频的点赞
     */
    private void disFavor(FineMessage message) {
        RelUserVideoFavor favor = message.getFavor();
        favorService.disFavor(favor);
    }

    /**
     * 用户分享视频
     */
    private void share(FineMessage message) {
        RelUserVideoShare share = message.getShare();
        shareService.share(share);
    }
}
